package com.jiyun.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiyun.mapper.MaintorderMapper;
import com.jiyun.pojo.Maintorder;
import com.jiyun.service.MaintorderService;
import com.jiyun.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class MaintorderServiceImpl extends ServiceImpl<MaintorderMapper, Maintorder> implements MaintorderService {

    @Autowired
    MaintorderMapper maintorderMapper;
    @Override
    public R findMaintorder(Maintorder maintorder) {
        List<Maintorder> list = maintorderMapper.findMaintorder(maintorder);
        return R.ok().data("list",list);
    }

    @Override
    public R findMaintByid(Integer id) {
        Maintorder maintorder = maintorderMapper.selectById(id);
        return R.ok().data("maintorder",maintorder);
    }

    @Override
    public R addMaintorder(Maintorder maintorder) {
        if(maintorder.getId()==null){
            Random random = new Random();
            StringBuilder randomNumber = new StringBuilder();
            for (int i = 0; i < 10; i++) {
                randomNumber.append(random.nextInt(10));
            }
            maintorder.setOid(randomNumber.toString());
            maintorder.setStatus(2);
            maintorder.setCjtime(new Date());
            maintorderMapper.insert(maintorder);
        }else {
            maintorderMapper.updateById(maintorder);
        }

        return R.ok();
    }

    @Override
    public R deleteMaintorder(Integer id) {
        maintorderMapper.deleteById(id);
        return R.ok();
    }
}
